************************************************************************
*************Table 6: Income Distribution: By community type ***********
************************************************************************

cd $PATH

* generate community type
use analysis_main.dta, clear

bys commid: egen aggregate_power=sum(so2_power_ann)
bys commid: egen aggregate_manu =sum(so2_manu_ann)


g type=.
replace type=1 if aggregate_power==0 & aggregate_manu==0  /*no emissions from manufacturing & power sector*/
replace type=2 if aggregate_power==0 & aggregate_manu>0   /* emissions from manufacturing sector only*/
replace type=3 if aggregate_power>0 & aggregate_manu==0   /* emissions from power sector only */
replace type=4 if aggregate_power>0 & aggregate_manu>0    /* emissions from both sectors */

tab type   /*only two types: 2 & 4*/

g commtype=1 if type==2
replace commtype=0 if type==4
label define commtype2 1 "manufacturing only" 0 "both sectors"
label values commtype commtype2

* regression
eststo clear
xtset commid WAVE
global score comm denc div econ health house market soc trans edc mart sani

local dependent loggini logtheil logpctratio_p90p10 logpctratio_p75p25

foreach y of local dependent{
eststo: xtreg `y' tcz##post $score i.WAVE if commtype==1, fe vce(cluster commid)  
eststo: xtreg `y' tcz##post $score i.WAVE if commtype==0, fe vce(cluster commid) 
eststo: xtreg `y' tcz##post##commtype $score i.WAVE, fe vce(cluster commid)
}


* generate Table 6

esttab est1 est4 est7 est10 using gini_commtype.tex, b(a2) se(a2) keep(1.tcz#1.post) stats(N r2_a, fmt(%9.0fc %9.3f) labels("Observations" "Adj. \(R^{2}\)"))  ///
starlevels(* 0.10 ** 0.05 *** 0.01) mtitles("Gini" "Theil" "P90/P10" "P75/P25") label nolines replace fragment  ///
prehead("\begin{tabular}{l*{4}{c}} \hline\hline") posthead("\hline \multicolumn{5}{l}{\textbf{A: Communities with emissions from the manufacturing sector only}} \\ ") 

esttab est2 est5 est8 est11 using gini_commtype.tex,  b(a2) se(a2) keep(1.tcz#1.post) stats(N r2_a, fmt(%9.0fc %9.3f) labels("Observations" "Adj. \(R^{2}\)")) ///
starlevels(* 0.10 ** 0.05 *** 0.01) label fragment append nomtitles nonumbers nolines

esttab est3 est6 est9 est12 using gini_commtype.tex,  b(a2) se(a2) keep(1.tcz#1.post#1.commtype) stats(N r2_a, fmt(%9.0fc %9.3f) labels("Observations" "Adj. \(R^{2}\)")) ///
starlevels(* 0.10 ** 0.05 *** 0.01) label fragment append nomtitles nonumbers nolines   ///
posthead("\hline \multicolumn{5}{l}{\textbf{B: Communities with emissions from both sectors}} \\") postfoot("\hline \end{tabular}") 



